package com.zqh.irmadscore.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zqh.irmadscore.model.entity.SdgChange;
import com.zqh.irmadscore.model.entity.SdgFailure;
import com.zqh.irmadscore.service.SdgChangeService;
import com.zqh.irmadscore.service.SdgFailureService;
import com.zqh.irmadscore.utils.resultUtils.Result;
import com.zqh.irmadscore.utils.resultUtils.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@AllArgsConstructor
@Tag(name = "SDG事故变化")
@RequestMapping("/sdg")
public class SDGController {


    private final SdgChangeService sdgChangeService;
    private final SdgFailureService sdgFailureService;


    @PostMapping("/setSdgChange")
    @Operation(summary = "一个监测道改变，故障的的发生概率改变")
    public Result<List<SdgFailure>> setSdgChange(@RequestParam Integer tractId,
                                                 @RequestParam Integer change) {

        try{

            List<SdgFailure> sdgFailureList = sdgChangeService.setSdgChange(tractId, change);

            return Result.ok(sdgFailureList);
        }
        catch (Exception e){
            return Result.fail(ResultCodeEnum.FAIL.getCode(),e.getMessage());
        }
    }

    @Operation(summary = "多个监测道改变，故障的的发生概率改变")
    @PostMapping("/setSdgChangeByArray")
    public Result<List<SdgFailure>> postArray(@RequestParam Integer[] tractIds,
                                              @RequestParam Integer[] change){

        List<SdgFailure> sdgFailureList = new ArrayList<>();

        try {
            for(var i = 0; i < tractIds.length; i++)
                sdgFailureList =    sdgChangeService.setSdgChange(tractIds[i],change[i]);

            return   Result.ok(sdgFailureList);
        }
        catch (Exception e){
            return Result.fail(ResultCodeEnum.FAIL.getCode(),e.getMessage());
        }

    }

    @GetMapping("/getSdgFailureByFailureName")
    @Operation(summary = "根据故障名字获取故障发生概率")
    public Result<SdgFailure> getSdgFailure(@RequestParam String failureName) {

        LambdaQueryWrapper<SdgFailure> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SdgFailure::getFailureName, failureName);

        SdgFailure sdgFailure = sdgFailureService.getOne(queryWrapper);

        return Result.ok(sdgFailure);

    }



    @Operation(summary = "查找所有事故发生概率")
    @GetMapping("/getAllSdgFailure")
    public Result<List<SdgFailure>> getAllSdgFailure() {


        return Result.ok(sdgFailureService.list());

    }


    @Operation(summary = "故障概率清零")
    @PutMapping("/updateSdgFailureForZero")
    public Result<T> updateSdgFailureForZero() {

        sdgFailureService.updateSdgFailureForZero();

        return Result.ok();

    }


}
